// Tableau.cpp: implementation of the CTableau class.
//
//////////////////////////////////////////////////////////////////////

#include "iface.h"
#include "Tableau.h"
#include "Buffer.h"

const char* CTableau::SlotName()
{
	return "Tableau";
}
CTableau::CTableau() 
{

}

CTableau::~CTableau()
{

}

bool CTableau::CheckPushPrecondition(const CCard &_card)
{

	if( false == CSlot::CheckPushPrecondition( _card ) ) return false;


	//Get a card from the top
	if( this->cards_stored == 0 )
	{
		if( _card.IsFaceDown() )
			return true;

		if( CCard::King == _card.GetValue() ) 
			return true;
		else
			return false;
	}
	else
	{
		CCard tmp = this->PeekCard();

		if
			(	false == _card.IsFaceDown() &&
				-1 == tmp.CompareValue( _card ) &&
				( tmp.GetColor() != _card.GetColor() )
			)
			return true;
		else
			return false;
	}
}

void CTableau::OpenCard()
{
	if( this->cardstack[ cards_stored-1 ].IsFaceDown() )
		this->cardstack[ cards_stored-1 ].Flip();
}

#define SPACE_CLOSED 5
#define SPACE_OPEN 20

void CTableau::CalculateDrawingArea( int& _x, int&  _y, int& _width, int& _height )
{
	// max 6 closed + 12 open 

	_width = CARD_WIDTH;
	_height = 6 * SPACE_CLOSED + 12 * SPACE_OPEN + CARD_HEIGHT;
	
}

void CTableau::Draw( unsigned int _x, unsigned int _y, bool selected )
{

	unsigned char i = 0;
	unsigned char closed = 0;

	ClearDrawingArea(_x, _y);

	for( i = 0; i < cards_stored; i++ )
	{
		cardstack[i].Draw( _x , _y + (SPACE_CLOSED * closed ) + (SPACE_OPEN * (i - closed )), false, 0,cardstack[i].IsFaceDown()?5:0  );
		if( cardstack[i].IsFaceDown() ) closed ++;
	}

	i--;

	if( cards_stored && cards_stored == closed-- )
	{
		cardstack[i].Draw( _x , _y + (SPACE_CLOSED * closed ) + (SPACE_OPEN * (i - closed )), false, 0,0  );
	}

	if( !cards_stored )
	{
		CCard::DrawPlaceHolder( _x, _y );
	}

	/* draw the hand (cursor) */

	if( selected )
	{

		closed = 0;
		for( i = 0; cards_stored && i < cards_stored - cards_selected + 1 ; i++ )
		{
			if( cardstack[i].IsFaceDown() ) closed ++;
		}

		if( cards_stored )
			CCard::DrawCursor( _x + SPACE_OPEN, _y + ( SPACE_CLOSED * closed ) + ( SPACE_OPEN * ( i - 1 - closed ) ) );
		else
			CCard::DrawCursor( _x + SPACE_OPEN, _y + 2 );


	}

}

bool CTableau::IncreaseSelection()
{
	if( cards_stored == 0 || cards_selected == cards_stored ) return false;

	if( false == cardstack[ cards_stored - cards_selected - 1].IsFaceDown() )
	{
		cards_selected++;
		return true;
	}

	return false;

}

bool CTableau::DecreaseSelection()
{
	if( cards_stored == 0 )
	{
		cards_selected = 0;
		return false;
	}

	if( cardstack[ cards_stored - 1 ].IsFaceDown() )
	{
		cards_selected = 0;
		return false;
	}

	if( 1 == cards_selected )
		return false;

	cards_selected--;
	return true;
}


//End of file


